package com.meixiang.activity.account.myfund;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.adapter.myFundAdapter.FundListFragmentPagerAdapter;
import com.meixiang.dialog.FundListSearchPopup;
import com.meixiang.fragment.fund.FundListFragment;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.view.ColumnHorizontalScrollView;
import com.meixiang.view.TitleView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的基金  --产品列表
 * Created by xyj on 2016/6/1.
 */
public class FundListMainActivity extends BaseActivity {

    @Bind(R.id.app_title)
    TitleView titleView ; //头部视图
    /** 自定义HorizontalScrollView */
    @Bind(R.id.mColumnHorizontalScrollView)
    ColumnHorizontalScrollView mColumnHorizontalScrollView;
    @Bind(R.id.mRadioGroup_content)
    LinearLayout mRadioGroup_content ;
    @Bind(R.id.shade_right)
    ImageView shade_right ;
    @Bind(R.id.ll_more_columns)
    LinearLayout ll_more_columns ;
    @Bind(R.id.rl_column)
    RelativeLayout rl_column ;
    @Bind(R.id.title_line)
    View title_line ;
    @Bind(R.id.mViewPager)
    ViewPager mViewPager ;
    private int mScreenWidth ; //屏幕的宽度
    /** 当前选中的栏目*/
    private int columnSelectIndex = 0;
    private FundListSearchPopup mSearchPopup ; //搜索选择层
    private final static String[] data = new String[]{"精品推荐","股票型","货币型","指数型","QDII型","混合型"};
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fund_list_content);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        mScreenWidth = Tool.getWindowWith(this);
}

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        ll_more_columns.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        setChangelView();
    }

    /**
     *  当栏目项发生变化时候调用
     * */
    private void setChangelView() {
        initColumnData();
        initTabColumn();
        initFragment();
    }

    /** 获取Column栏目 数据*/
    private void initColumnData() {
        titleView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.fund_money_color));
        titleView.setTitle("基金");
        titleView.setLeftImageButton(R.mipmap.month_3);
        titleView.setTColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     *  初始化Column栏目项
     * */
    private void initTabColumn() {
        mRadioGroup_content.removeAllViews();
        int count =  data.length; //获取title的长度
        mColumnHorizontalScrollView.setParam(this, mScreenWidth, mRadioGroup_content, shade_right, ll_more_columns, rl_column);
        for(int i = 0; i< count; i++){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT , ViewPager.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 20;
            params.rightMargin = 20;
            TextView columnTextView = new TextView(this);
            columnTextView.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.black_3));
            columnTextView.setGravity(Gravity.CENTER);
            columnTextView.setPadding(5, 10, 5, 10);
            columnTextView.setId(i);
            columnTextView.setTextSize(13);
            columnTextView.setText(data[i]);
            if(columnSelectIndex == i){
                columnTextView.setSelected(true);
                columnTextView.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.fund_money_color));
                columnTextView.setTextSize(15);
            }
            columnTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for(int i = 0;i < mRadioGroup_content.getChildCount();i++){
                        View localView = mRadioGroup_content.getChildAt(i);
                        TextView text = (TextView) mRadioGroup_content.getChildAt(i);
                        if (localView != v) {
                            localView.setSelected(false);
                            text.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.black_3));
                            text.setTextSize(13);
                        }else{
                            localView.setSelected(true);
                            text.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.fund_money_color));
                            text.setTextSize(15);
                            mViewPager.setCurrentItem(i);
                        }
                    }
                }
            });
            mRadioGroup_content.setGravity(Gravity.CENTER_VERTICAL);
            mRadioGroup_content.addView(columnTextView, i ,params);
        }
    }

    /**
     *  选择的Column里面的Tab
     * */
    private void selectTab(int tab_postion) {
        columnSelectIndex = tab_postion;
        for (int i = 0; i < mRadioGroup_content.getChildCount(); i++) {
            View checkView = mRadioGroup_content.getChildAt(tab_postion);
            int k = checkView.getMeasuredWidth();
            int l = checkView.getLeft();
            int i2 = l + k / 2 - mScreenWidth / 2;
            mColumnHorizontalScrollView.smoothScrollTo(i2, 0);
        }
        //判断是否选中
        for (int j = 0; j <  mRadioGroup_content.getChildCount(); j++) {
            View checkView = mRadioGroup_content.getChildAt(j);
            TextView text = (TextView) mRadioGroup_content.getChildAt(j);
            boolean ischeck;
            if (j == tab_postion) {
                ischeck = true;
                text.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.fund_money_color));
                text.setTextSize(15);
            } else {
                ischeck = false;
                text.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.black_3));
                text.setTextSize(13);
            }
            checkView.setSelected(ischeck);
        }
    }
    /**
     *  初始化Fragment
     * */
    private void initFragment() {
        int count =  data.length;
        for(int i = 0; i< count;i++){
            Bundle data = new Bundle();
            data.putInt("index",i);
            fragments.add(new FundListFragment().getIntance(data));
        }
        FundListFragmentPagerAdapter mAdapetr = new FundListFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(mAdapetr);
        mViewPager.addOnPageChangeListener(pageListener);
    }

    /**
     *  ViewPager切换监听方法
     * */
    public ViewPager.OnPageChangeListener pageListener= new ViewPager.OnPageChangeListener(){

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int position) {
            mViewPager.setCurrentItem(position);
            selectTab(position);
        }
    };

    /**
     * 初始化搜索视图PopupWindow
     */
    private void initPupopWindow() {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_fund_list_search_view, null);
        mSearchPopup = new FundListSearchPopup(context,view,this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_more_columns: //初始化popup并显示
                initPupopWindow();
                if(mSearchPopup.isShowing()){
                    mSearchPopup.dismiss();
                }else{
                    mSearchPopup.showAsDropDown(title_line);
                }
                break ;
        }
    }
}
